package cn.bulibuqi.myblog.controller;

import cn.bulibuqi.myblog.bean.Blog;
import cn.bulibuqi.myblog.bean.User;
import cn.bulibuqi.myblog.service.BlogService;
import cn.bulibuqi.myblog.service.TagService;
import cn.bulibuqi.myblog.service.TypeService;
import cn.bulibuqi.myblog.service.TypeServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-12-21:00
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String list(
            @RequestParam(required = false, defaultValue = "1") Integer current,  //必须String，框架会自动转换
            @RequestParam(required = false, defaultValue = "3") Integer size,
            Blog blog,
            Model model){

        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(new Page(current,size),blog));


        return "admin/blogs";
    }


    @PostMapping("/blogs/search")
    public String search(
            @RequestParam(required = false, defaultValue = "1") Integer current,  //必须String，框架会自动转换
            @RequestParam(required = false, defaultValue = "2") Integer size,
            Blog blog,
            Model model){



        model.addAttribute("page",blogService.listBlog(new Page(current,size),blog));
        //渲染table页面
        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("blog",new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());


        return "admin/blogs-input";
    }


    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        Blog blog = blogService.getBlog(id);
        blog.init();

        model.addAttribute("blog",new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",blog);

        return "admin/blogs-input";
    }



    @PostMapping("/blogs")
    public String post(Blog blog,
                       HttpSession session){
        //以后重写saveBlog 返回bool
        User user = (User) session.getAttribute("user");
        //保存用户信息
        blog.setUserId((user.getId()));
        List<Long> list = blog.stringToList(blog.getTagIds());
        System.out.println(list);
        blogService.saveAndUpdate(blog);

        return "redirect:/admin/blogs";
    }


    @GetMapping("/blogs/{id}/{current}/delete")
    public String delete(@PathVariable Long id,@PathVariable Integer current, RedirectAttributes attributes){

        blogService.deleteBlog(id);

        attributes.addFlashAttribute("message","删除成功");
        //给跳转的页面加入参数
        attributes.addAttribute("current",current);
        return "redirect:/admin/blogs";
    }






}
